package model.events

import model.postgresql.{PgBaseEvent, PgGroupJoin}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}
import zio.json.*

import java.time.ZonedDateTime

case class GroupJoin(
    `object`: GroupJoin.`object`,
    event_id: Option[String],
    timestamp: Option[Long]
) extends BaseEvent(event_id, timestamp) {
  override def convertToJson: String             = this.toJson
  override def getUserId: Some[Long]             = Some(this.`object`.user_id)
  override def setTimestamp(ts: Long): GroupJoin = copy(timestamp = Some(ts))

  override def toPg: Option[PgBaseEvent] =
    for {
      eventId   <- event_id
      timestamp <- timestamp
    } yield PgGroupJoin(
      eventId,
      `object`.join_type,
      timestamp,
      `object`.user_id
    )
}

object GroupJoin {
  case class `object`(user_id: Long, join_type: String)

  given JsonDecoder[`object`] = DeriveJsonDecoder.gen[`object`]
  given JsonEncoder[`object`] = DeriveJsonEncoder.gen[`object`]

  given JsonDecoder[GroupJoin] = DeriveJsonDecoder.gen[GroupJoin]
  given JsonEncoder[GroupJoin] = DeriveJsonEncoder.gen[GroupJoin]
}
